package com.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.R;
import com.app.http.HttpDataApi;
import com.app.view.LoadingDialog;
import com.umeng.analytics.MobclickAgent;


public abstract class ActivityBase extends AppCompatActivity implements HttpDataApi.OnRequestCallback {

    protected ImageView ivBack;
    protected TextView tvTitle;

    protected LinearLayout mContentView;

    protected RelativeLayout headView;

    // 用于显示 Loading Dialog
    public LoadingDialog mDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initRootView();
    }

    protected abstract void initRootView();



    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        getLayoutInflater().inflate(layoutResID, mContentView);
    }

    @Override
    public void setContentView(View view) {
        setContentView(view, view.getLayoutParams());
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, 0);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, R.anim.push_left_out);
    }


    public void setIvBackImageRes(int ResId) {
        ivBack.setImageResource(ResId);
    }

    public void setIvBackClickListener(View.OnClickListener listener) {
        ivBack.setOnClickListener(listener);
    }

    public void setIvBackPadding(int left, int top, int right, int bottom) {
        ivBack.setPadding(left, top, right, bottom);
    }


    public void setTitle(CharSequence title) {
        tvTitle.setText(title);
    }

    public void setTitleClickListener(View.OnClickListener listener) {
        tvTitle.setOnClickListener(listener);
    }

    public void setNavigationDrawable(int drawableId) {
        ivBack.setImageResource(drawableId);
    }

    public void setTitleColor(int colorId) {
        tvTitle.setTextColor(colorId);
    }

    public void setHeadViewBackgroundColor(@ColorRes int colorId) {
        headView.setBackgroundColor(getResources().getColor(colorId));
    }



    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    /**
     * @return
     */
    public RelativeLayout getHeadView() {
        return headView;
    }

    /**
     * 移除左端返回按钮
     */
    public void removemNavigationView() {
        ivBack.setVisibility(View.INVISIBLE);
    }

    /**
     * 显示加载页面
     *
     * @return 当前的对话框对象
     */
    public void showLoadingDialog() {
        showLoadingDialog("数据加载中，请稍候...");
    }

    /**
     * 显示加载页面
     *
     * @param loadingText 加载提示文字
     * @return 当前的对话框对象
     */
    public void showLoadingDialog(String loadingText) {
        if (mDialog == null) {
            mDialog = new LoadingDialog(this);
        }
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
        mDialog.setTitle(loadingText);
        mDialog.show();
    }

    public void dissmissLoadingDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }


}
